package com.bitrate.dagger.sdk.mqtt.sender;

import com.bitrate.dagger.sdk.mqtt.dto.MqttPushDTO;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: MQTT Sender
 * @version v1.0
 * @author 刘小龙
 * @date 2023年12月6日上午9:37:55
 */
@Slf4j
@Data
public class MqttSender {

	private String host;// MQTT平台的请求地址

	private String commonKey;// MQTT平台颁发的签名

	public MqttSender(String host, String commonKey) {
		this.host = host + "/mqtt-api/publish";
		this.commonKey = commonKey;
	}

	public String push(String clientId, String topic, String message) {
		MqttPushDTO pushDTO = new MqttPushDTO();
		pushDTO.setCommonKey(commonKey);
		pushDTO.setClientId(clientId);
		pushDTO.setTopic(topic);
		pushDTO.setMessage(message);
		String jsonBody = JSONUtil.toJsonStr(pushDTO);
		log.info("MQTT推送请求参数:{}", jsonBody);
		HttpResponse response = HttpRequest.post(this.host).body(jsonBody).execute();
		return response.body();
	}

}
